package com.firewolf.permission.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.firewolf.permission.dao.FunctionMapper;
import com.firewolf.permission.dao.RoleMapper;
import com.firewolf.permission.dao.UserMapper;
import com.firewolf.permission.entities.Function;
import com.firewolf.permission.entities.FunctionExample;
import com.firewolf.permission.entities.Role;
import com.firewolf.permission.entities.RoleExample;
import com.firewolf.permission.entities.User;
import com.firewolf.permission.entities.UserExample;
import com.firewolf.permission.ui.EasyUIGridData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class UserService {

	@Value("${root.name}")
	private String rootName;
	@Value("${root.pwd}")
	private String rootPwd;

	@Autowired
	private UserMapper uMapper;

	@Autowired
	private FunctionMapper fMapper;

	@Autowired
	private RoleMapper rMapper;

	/**
	 * 登录
	 * 
	 * @param user
	 * @return
	 */
	public User findUserForLogin(User user) {

		UserExample ue = new UserExample();
		ue.createCriteria().andAccountEqualTo(user.getAccount()).andPasswordEqualTo(user.getPassword());
		List<User> uList = uMapper.selectByExample(ue);
		if (uList == null || uList.size() == 0) {
			if (rootName.equals(user.getAccount()) && rootPwd.equals(user.getPassword())) {
				user.setName("超级管理员");
				return user;
			}
			return null;
		}
		return uList.get(0);
	}

	/**
	 * 查找用户的功能
	 * 
	 * @param user
	 * @return
	 */
	public List<Function> selectFunctions(User user) {
		
		if(!rootName.equals(user.getAccount())){
			List<Integer> roleIds = uMapper.findRoleIdsByUid(user.getId());
			List<Integer> fIds = fMapper.findIdsByRoleIds(roleIds);
			FunctionExample ex = new FunctionExample();
			ex.createCriteria().andIdIn(fIds);
			List<Function> fList = fMapper.selectByExample(ex);
			return fList;
		}
		List<Function> fList = fMapper.selectByExample(new FunctionExample());
		return fList;
	}

	/**
	 * 查找用户，带有分页
	 * 
	 * @param page
	 * @param rows
	 * @return
	 */
	public EasyUIGridData finUsers(Integer page, Integer rows) {
		PageHelper.startPage(page, rows);
		List<User> uList = uMapper.selectByExample(new UserExample());
		PageInfo<User> info = new PageInfo<>(uList);
		long total = info.getTotal();
		EasyUIGridData data = new EasyUIGridData(total, uList);
		return data;
	}

	/**
	 * 添加用户
	 * 
	 * @param u
	 * @return
	 */
	public int addUser(User u) {
		u.setStatus(1); // 用户是启用状态
		u.setPassword("111111");
		return uMapper.insert(u);
	}

	/**
	 * 批量删除用户
	 * 
	 * @param ids
	 * @return
	 */
	public int deleteBatch(String ids) {
		UserExample ue = new UserExample();
		List<Integer> idList = new ArrayList<Integer>();
		String[] split = ids.split(",");
		for (String id : split) {
			idList.add(Integer.valueOf(id));
		}
		ue.createCriteria().andIdIn(idList);
		return uMapper.deleteByExample(ue);
	}

	/**
	 * 更新用户的角色
	 * 
	 * @param roleids
	 * @param userid
	 * @return
	 */
	public int updateRoles(String roleids, Integer userid) {
		// 先删除当前用户的所有角色
		uMapper.deleteRolesBath(userid);
		String[] split = roleids.split(",");
		int count = 0;
		for (String s : split) {
			int c = uMapper.addRole(Integer.parseInt(s), userid);
			count += c;
		}
		return count;
	}

	/**
	 * 根据用户查询角色 查询出所有的角色，如果用户已经有对应角色，则添加checked为false，用于前台树的回显
	 * 
	 * @param user
	 * @return
	 */
	public List<Role> findAllRoles(Integer userId) {
		List<Integer> uRoleIds = uMapper.findRoleIdsByUid(userId);
		List<Role> allRoles = rMapper.selectByExample(new RoleExample());
		for (Role r : allRoles) {
			if (uRoleIds.contains(r.getId())) {
				r.setChecked(true);
			}
		}
		return allRoles;
	}
}
